package com.sc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sc.domain.Person;
import com.sc.dto.CheckDto;
import com.sc.dto.PersonDto;
import com.sc.mapper.PersonMapper;
import com.sc.service.PersonService;
import com.sc.vo.PersonDeatilVo;
import com.sc.vo.PersonLabelVo;
import com.sc.vo.PersonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Rui
 * @Date: 2024/11/28 20:58
 */
@Service
@Transactional
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public boolean deleteById(Integer id) {
        return personMapper.deleteById(id);
    }

    @Override
    public boolean add(Person person) {
        return personMapper.add(person);
    }

    @Override
    public boolean update(Person person) {
        return personMapper.update(person);
    }

    @Override
    public Map<String, Object> list(PersonDto personDto) {
        PageHelper.startPage(personDto.getPage(), personDto.getPageSize());
        Page<PersonVo> info = personMapper.list(personDto);
        Map<String, Object> map = new HashMap<>();
        map.put("total", info.getTotal());
        map.put("pageSize", info.getPageSize());
        map.put("pageNumber", info.getPageNum());
        map.put("data", info.getResult());

        return map;
    }

    @Override
    public Map<String, Object> selectDetailById(Integer id) {
        PersonDeatilVo personDeatilVo = personMapper.selectDetailById(id);
        List<PersonLabelVo> personLabelVos = personMapper.selectLabelByPersonId(id);
        System.out.println(personLabelVos);
        System.out.println(personDeatilVo);
        Map<String, Object> map = new HashMap<>();
        map.put("person", personDeatilVo);
        map.put("label", personLabelVos);

        return map;
    }

    @Override
    public boolean updateCheck(CheckDto checkdto) {
        if (checkdto.getLabelIds() != null && !checkdto.getLabelIds().isEmpty()) {
            personMapper.deleteLabel(checkdto.getId());
            for (Integer labelId : checkdto.getLabelIds()) {
                personMapper.addLabel(checkdto.getId(), labelId);
            }
        }
        return personMapper.updateCheck(checkdto);
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        return personMapper.deleteByIds(ids);
    }

    @Override
    public Person selectById(Integer id) {
        return personMapper.selectById(id);
    }

    @Override
    public Map<String, Object> selectDetailByName(String name) {
        // 根据姓名查找住户详细信息
        PersonDeatilVo personDeatilVo = personMapper.selectDetailByName(name);
        List<PersonLabelVo> personLabelVos = personMapper.selectLabelByPersonId(personDeatilVo.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("person", personDeatilVo);
        map.put("label", personLabelVos);

        return map;
    }

}
